<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="test"></div>

  <script src="../js17/react.development.js"></script>
  <script src="../js17/react-dom.development.js"></script>
  <script src="../js17/babel.min.js"></script>

  <script type="text/babel">
    class Count extends React.Component {

      constructor (props) {
        console.log ('constructor')
        super (props)
        this.state = {count: 0}
      }
      
      UNSAFE_componentWillMount () {
        console.log ('componentWillMount')
      }

      // 组件挂载完毕
      componentDidMount () {
        console.log ('componentDidMount')
      }
      
      // 组件将要卸载
      componentWillUnmount () {
        console.log ('componentWillUnmount')
      }

      // 控制组件更新
      shouldComponentUpdate () {
        console.log ('shouldComponentUpdate')
        return true
      }

      // 组件将要更新
      UNSAFE_componentWillUpdate () {
        console.log ('componentWillUpdate')
      }

      // 组件更新完毕
      componentDidUpdate () {
        console.log ('componentDidUpdate')
      }

      add = () => {
        const {count} = this.state
        this.setState ({count: count+1})
      }

      death = () => {
        // 卸载组件
        ReactDOM.unmountComponentAtNode (document.getElementById ('test'))
      }

      force = () => {
        this.forceUpdate ()
      }

      // 初始化渲染 状态更新之后
      render () {
        console.log ('render')
        return (
          <div>
            <h2>当前求和为:{this.state.count}</h2>
            <button onClick={this.add}>+1</button>
            <button onClick={this.death}>卸载</button>
            <button onClick={this.force}>forceUpdate</button>
          </div>
        )
      }
    }
   
    class A extends React.Component {
      state = {carName: 'audi'}

      changeCar = () => {
        this.setState ({carName: 'benz'})
      }
      render () {
        return (
          <div>
            <h2>我是A组件</h2>
            <B carName={this.state.carName}></B>
            <button onClick={this.changeCar}>换车</button>
          </div>
        )
      }
    }

    class B extends React.Component {

      UNSAFE_componentWillReceiveProps () {
        console.log ('componentWillReceiveProps')
      }

      render () {
        return (
          <div>
            <h2>我是B组件, 展示的车是： {this.props.carName}</h2>
          </div>
        )
      }
    }

    // 渲染到页面
    ReactDOM.render (<Count/>, document.getElementById ('test'))
  </script>
</body>
</html>